package defview;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

import utils.Globals;
import utils.Images;
import zq.cn.activity.GameActivity;
import zq.cn.activity.MainActivity;

/**
 * Created by Administrator on 2016/4/25.
 */
public class MyView extends View {

    private GameActivity g;
    private int[] index;
    private String imgAnswer;
    private String[] userAnswer;
    private float leftX;
    public MyView(Context context) {
        super(context);
    }

    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        g = (GameActivity)context;

        this.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        this.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    float x = event.getX();
                    if (x > leftX) {
                        //点击第一个网格文字时，满足
                        // 0<x-leftX<Globals.GRID_SIZE -->0<i<1
                        //同理，点击其他网格内容时，满足上面基本条件
                        int i = (int) (x - leftX) / (Globals.GRID_SIZE + Globals.GRID_SEP);
                        if (userAnswer[i] != null) {
                            userAnswer[i] = null;
                            //根据索引获取指定的View
//                            View vi = g.getValues().get(index[i]);
                            v = g.getValues().get(index[i]);
                            v.setVisibility(View.VISIBLE);
                            index[i] = -1;
                            postInvalidate();
                        }
                    }
                }
                return false;
            }
        });
    }

    public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void initAnswer(String imgAnswer) {
        this.imgAnswer = imgAnswer;
        userAnswer = new String[imgAnswer.length()];
        index = new int[imgAnswer.length()];
        postInvalidate();
    }

    public void initUserAnswer(String uAnswer, int position) {
        for(int i=0;i<userAnswer.length;i++) {
            if(userAnswer[i]==null) {
                userAnswer[i] = uAnswer;
                index[i] = position;
                break;
            }
        }
        StringBuffer sb = new StringBuffer();
        //userAnswer.length==imgAnswer.length()
        for(int i=0;i<userAnswer.length;i++) {
            if(userAnswer[i]==null) {
                break;
            }
            sb.append(userAnswer[i]);
        }

        if(sb.length()==imgAnswer.length() && sb.toString().equals(imgAnswer)) {
            Toast.makeText(g, "答案正确，跳至下一关", Toast.LENGTH_SHORT).show();
            SharedPreferences s = g.getSharedPreferences("game", g.MODE_PRIVATE);
            SharedPreferences.Editor e =s.edit();
            e.putInt("nowStage",s.getInt("nowStage", 0)+1);
            e.putInt("coin", s.getInt("coin", 100) + 50);
            e.commit();
            Intent in = new Intent(g, GameActivity.class);
            in.putExtra("stageNum",s.getInt("nowStage",0));
            g.startActivity(in);
            g.finish();
        }

        postInvalidate();
    }
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint paint = new Paint();
        paint.setTextSize(30);
        paint.setColor(Color.WHITE);
        if(imgAnswer!=null) {
            leftX = Globals.SCREEN_WIDTH/2-(imgAnswer.length()*Globals.GRID_SIZE/2+(int)(imgAnswer.length()/2)*Globals.GRID_SEP);
            for (int i=0;i<imgAnswer.length();i++) {
                canvas.drawBitmap(Images.getBgImg(), leftX+i*(Globals.GRID_SIZE+Globals.GRID_SEP), 8, paint);
                if(userAnswer[i]!=null) {
                    canvas.drawText(userAnswer[i], leftX+i*(Globals.GRID_SIZE+Globals.GRID_SEP)+(Globals.GRID_SIZE/2-25),
                            Globals.GRID_SIZE/2+7, paint);
                }
            }
        }
    }
}
